<?php
namespace app\admin\controller;

use think\Db;
use think\Request;

class Activity extends Common
{

    // 活动管理
    public function index()
    {
        if (request()->isPost()) {

            $where = array();
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('common.page_sizes');
            $list = db('activity')
                ->where($where)
                ->order('id desc')
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            $list = model('Activity')->listTimeInfo($list);
            return $result = ['code' => 0, 'msg' => config('common.data_success'), 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return $this->fetch('');
    }
    // 活动删除
    public function del()
    {
        if (request()->isPost()) {
            $data = input('post.')['id'];
            return model('Activity')->commonDel($data, '删除成功', '删除失败，请重试');
        }
    }

    public function add()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $post['addtime'] = time();
            return model('Activity')->commonData($post, '添加成功', '添加失败，请重试');

        } else {
            return $this->fetch('');
        }
    }

    public function edit()
    {
        if (request()->isPost()) {
            $post = input('post.');
          
            return model('coupon_user')->commonData($post, '添加成功', '添加失败，请重试');
            
        } else {
            $id = input('id');
            $list = db('activity')->where(['id'=>$id])->find();
            if($list['cate_id']==3){
                $table = 'coupon';
            }
            if($list['cate_id']==4){
                $table = 'second_card';
            }
            if($list['cate_id']==5){
                $table = 'recharge_card';
            }
            if($list['cate_id'] == 2){
                $list_category = db('room')
                    ->alias('r')
                    ->join('room_cate c','c.id = r.two_cate')
                    ->where(['r.is_activity'=>1])
                    ->field('r.id as id,c.title as title')
                    ->select();
            }else{
                if($list['cate_id'] != 1){
                    $list_category = db($table)->where(['status' => 0,'is_activity'=>1])->order('id asc')->select();
                }else{
                    $list_category = [];
                }
            }
            if($list['param']!=0){
                $list['param']=explode(',',$list['param']);
            }
            return $this->fetch('', ['list_category' => $list_category,'list'=>$list]);
        }
    }
    public function getparam()
    {
        if (request()->isPost()) {
            $cate_id = input('post.cate_id');
            if ($cate_id != 1) {
                if ($cate_id == 2) {
                    $table = 'room';
                    $list = db($table)
                        ->alias('r')
                        ->join('room_cate c', 'r.two_cate = c.id')
                        ->where(['r.is_activity' => 1])
                        ->field('r.id as id,c.title as title')
                        ->order('r.id desc')
                        ->select();
                } else {
                    if ($cate_id == 3) {
                        $table = 'coupon';
                    }
                    if ($cate_id == 4) {
                        $table = 'second_card';
                    }
                    if ($cate_id == 5) {
                        $table = 'recharge_card';
                    }
                    $list = db($table)
                        ->where(['is_activity' => 1])
                        ->order('id desc')
                        ->select();
                }

            } else {
                $list = [];
            }
            return $result = ['code' => 0, 'msg' => config('common.data_success'), 'data' => $list];
        }
    }
    public function status()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('Activity')->commonState($data, '状态更变成功', '状态更变失败，请稍后重试');

        }
    }

}
